#include "udp_libevent.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <winsock2.h>  
#include <ws2tcpip.h> 
#ifdef __cplusplus
extern "C" {
#endif
#include <event.h>
#include <event2/listener.h>

#ifdef __cplusplus
}
#endif
#define MSG_LEN 1024

void read_cb(evutil_socket_t fd, short event, void* arg) {
	UdpDataGramLibevent* parent = (UdpDataGramLibevent*)arg;
	if (parent == nullptr)
		return;

	char buf[MSG_LEN];
	int len;
	int addr_len = sizeof(struct sockaddr);
	struct sockaddr_in cli_addr;

	memset(buf, 0, sizeof(buf));
	len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&cli_addr, &addr_len);

	if (len == -1) {
		perror("recvfrom");
	}
	else if (len == 0) {
		printf("Connection Closed\n");
	}
	else {
		buf[len] = '\0';
		printf("recv[%s:%d]\n", buf, len);
		// sendto(fd, buf, len, 0, (struct sockaddr*)&cli_addr, addr_len);
		if (parent->OnReadHandle() != nullptr) {
			UdpDataGramLibevent::OnReadDataHandle p = (parent->OnReadHandle());
			if(p)
				p(buf, len, cli_addr);
		}
	}
}

int UdpDataGramLibevent::bind_socket(struct event* ev, const char* ip, uint16_t port) {
	int sock_fd;
	int flag = 1;
	struct sockaddr_in local_addr;

	if ((nullptr == ip) || (nullptr == ev)) {
		m_status = FAIL;
		return -1;
	}

	if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket");
		return -1;
	}

	if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&flag, sizeof(flag)) < 0) {
		perror("setsockopt");
		return 1;
	}
	memset(&local_addr, 0, sizeof(local_addr));
	local_addr.sin_family = AF_INET;
	local_addr.sin_port = htons(port);
	local_addr.sin_addr.s_addr = inet_addr(ip);
	if (bind(sock_fd, (struct sockaddr*)&local_addr, sizeof(struct sockaddr)) < 0) {
		perror("bind");
		return -1;
	}
	else {
		printf("bind success, port[%d]\n", port);
	}
	// 加入组播  
	ip_mreq multiCast;
	multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
	// multiCast.imr_interface.S_un.S_addr = inet_addr("192.168.0.104");
	multiCast.imr_multiaddr.S_un.S_addr = inet_addr("239.2.2.2");
	int iRet = setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
	if(iRet != 0){
		printf("setsockopt fail:%d", WSAGetLastError());
		return -1;
	}

	event_set(ev, sock_fd, EV_READ | EV_PERSIST, &read_cb, (void*)this);
	if (event_add(ev, NULL) == -1) {
		perror("event_set");
	}
	m_sock_fd = sock_fd;
	return 0;
}


int UdpDataGramLibevent::bind_socket_with_group(struct event* ev, const char* ip, uint16_t port,const char *group_addr) {
	int sock_fd;
	int flag = 1;
	struct sockaddr_in local_addr;

	if ((nullptr == ip) || (nullptr == ev)) {
		m_status = FAIL;
		return -1;
	}

	if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket");
		return -1;
	}

	if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&flag, sizeof(flag)) < 0) {
		perror("setsockopt");
		return 1;
	}
	memset(&local_addr, 0, sizeof(local_addr));
	local_addr.sin_family = AF_INET;
	local_addr.sin_port = htons(port);
	local_addr.sin_addr.s_addr = inet_addr(ip);
	if (bind(sock_fd, (struct sockaddr*)&local_addr, sizeof(struct sockaddr)) < 0) {
		perror("bind");
		return -1;
	}
	else {
		printf("bind success, port[%d]\n", port);
	}
	// 加入组播  
	ip_mreq multiCast;
	multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
	// multiCast.imr_interface.S_un.S_addr = inet_addr("192.168.0.104");
	multiCast.imr_multiaddr.S_un.S_addr = inet_addr(group_addr);
	int iRet = setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
	if(iRet != 0){
		printf("setsockopt fail:%d", WSAGetLastError());
		return -1;
	}

	event_set(ev, sock_fd, EV_READ | EV_PERSIST, &read_cb, (void*)this);
	if (event_add(ev, NULL) == -1) {
		perror("event_set");
	}
	m_sock_fd = sock_fd;
	return 0;
}


UdpDataGramLibevent::OnReadDataHandle UdpDataGramLibevent::OnReadHandle() {
	return this->mOnRead;
}

void UdpDataGramLibevent::SetOnReadHandle(UdpDataGramLibevent::OnReadDataHandle p) {
	this->mOnRead = p;
}

void UdpDataGramLibevent::SendTo(const char* dat, uint32_t len, std::string ip, int port) {
	struct sockaddr_in dest_addr;
	memset(&dest_addr, 0, sizeof(dest_addr));
	dest_addr.sin_family = AF_INET;
	dest_addr.sin_port = htons(port);
	dest_addr.sin_addr.s_addr = inet_addr(ip.c_str());
	int addr_len = sizeof(struct sockaddr);
	if (m_sock_fd > 0) {
		sendto(m_sock_fd, (const char*)dat, len, 0, (struct sockaddr*)&dest_addr, addr_len);
	}
}

int UdpDataGramLibevent::SocketFD() {
	return this->mSocketFD;
}


UdpDataGramLibevent::
UdpDataGramLibevent(std::string ip, uint32_t port)
{
	m_status = STOP;
	m_bind_ip = ip;
	m_port = port;
	if (event_init() == NULL) {
		printf("event_init() failed\n");
	}
	m_event = new struct event;
	if (0 > bind_socket(m_event, ip.c_str(), port)) {
		return;
	}

	m_thread = new std::thread([this]() {
		event_dispatch();
		}
	);
	m_status = RUNNING;
}

UdpDataGramLibevent::UdpDataGramLibevent(std::string ip, uint32_t port,std::string group_addr)
{
	m_status = STOP;
	m_bind_ip = ip;
	m_port = port;
	if (event_init() == NULL) {
		printf("event_init() failed\n");
	}
	m_event = new struct event;
	if (0 > bind_socket_with_group(m_event, ip.c_str(), port,group_addr.c_str())) {
		return;
	}

	m_thread = new std::thread([this]() {
		event_dispatch();
		}
	);
	m_status = RUNNING;
}



